package practice3;
import java.net.*;
import java.nio.charset.StandardCharsets;

public class UDPclient {
    public static void main(String[] args) throws Exception {
        DatagramSocket client = new DatagramSocket();
        String message = "外星人，你好，我是廖禧！"; // 原消息
        byte[] sendData = message.getBytes(StandardCharsets.UTF_8); // 用UTF-8编码成字节数组
        // 发送时使用字节数组的实际长度（sendData.length）
        DatagramPacket packet = new DatagramPacket(
                sendData,
                sendData.length,
                InetAddress.getByName("localhost"),
                9999
        );
        client.send(packet);
        System.out.println("【Client】：" + message);
        System.out.println("【Client】发送字节数：" + sendData.length); // 打印发送的字节数，对比接收端
        client.close();
    }
}